﻿var startStatus = "";
function InitData(obj) {
    //todo
    //状态是已完成  进度为100
    //负责人可修改：抄送，任务状态，任务进度，任务属性
    //下达人可以修改：负责人，抄送人，开始/结束时间、任务状态，任务进度，任务属性
    mini.get("txt_task_name_").setValue(obj.task_name_);
    mini.get("txt_assign_user_name_").setValue(obj.assign_user_id_);
    mini.get("txt_assign_user_name_").setText(obj.assign_user_name_);
    mini.get("txt_manage_user_name_").setValue(obj.manage_user_id_);
    mini.get("txt_manage_user_name_").setText(obj.manage_user_name_);
    mini.get("txt_estimated_start_time_").setValue(obj.estimated_start_time_);
    mini.get("txt_estimated_end_time_").setValue(obj.estimated_end_time_);
    if (obj.status_ == 0) {
        obj.status_ = 1;
    }
    startStatus = obj.status_;
    mini.get("cmbTaskStatus").setValue(obj.status_);
    mini.get("spTaskProgress").setValue(obj.progress_);
    mini.get("txt_description_").setValue(obj.description_);

    InitTaskCategory(obj.category_id_, "txt_category_id_");
    InitTaskProperty(obj.task_id_, "txt_task_property_");
    InitTaskCc(obj.task_id_, "txt_cc");
    $.ajax({
        url: "/AJAX/AjaxService.aspx?method=GetSession",
        type: "post",
        cache: false,
        success: function (text) {
            var user = mini.decode(text);

            //GetReportHistory(obj.task_id_, (obj.assign_user_id_ == user.UserID || obj.manage_user_id_ == user.UserID || obj.assist_user_id_ == user.UserID));
            InitTaskTimeLine(obj.task_id_, (obj.manage_user_id_ == user.UserID) && (obj.status_ == 1), 1, obj.estimated_start_time_, 1);
            InitControl(obj.assign_user_id_, obj.manage_user_id_, obj.assist_user_id_, user.UserID);
        }
    });
}

function InitControl(assign_user_id_, manage_user_id_, assist_user_id_, UserID) {
    if (UserID == assign_user_id_) {
        mini.get("txt_task_name_").enable();
        mini.get("txt_manage_user_name_").enable();
        mini.get("txt_estimated_start_time_").enable();
        mini.get("txt_estimated_end_time_").enable();
        mini.get("cmbTaskStatus").enable();
        mini.get("spTaskProgress").enable();
        mini.get("txt_description_").enable();
        mini.get("txt_category_id_").enable();
        mini.get("txt_task_property_").enable();
        mini.get("txt_cc").enable();
    } else if (UserID == manage_user_id_ || UserID == assist_user_id_) {
        mini.get("cmbTaskStatus").enable();
        mini.get("spTaskProgress").enable();
        mini.get("txt_task_property_").enable();
        //mini.get("txt_cc").enable();
    } else {
        mini.get("save").disable();
    }
}

function GetReportHistory(taskID, canCommunication) {
    if (canCommunication)
        $("#d_report").append('<table style="background-color: #F0F0F0; padding-top: 10px;width:99%" id="t_communication"><tr><td class="tdleft">批注内容：</td><td><input id="txt_content" style="width: 99%; height: 80px" class="mini-textarea" /></td></tr></table>');
    mini.parse();
    $.ajax({
        url: "/AJAX/AjaxService.aspx?method=GetTaskCommunicationHistory",
        cache: false,
        type: "post",
        data: { task_id_: taskID },
        success: function (text) {
            var obj = mini.decode(text);
            var layer = '';
            for (var i = 0; i < obj.length; i++) {
                layer += '<table class="tasking_report"><tr><td class="task_time_hover">' + new Date(obj[i].update_date_).Format("yyyy-MM-dd hh:mm:ss") + '</td></tr><tr><td class="task_com_info"><strong>' + obj[i].user_name_ + '</strong>: ' + obj[i].content_ + '</td></tr></table>';
            }
            if (layer.length <= 0) {
                layer = '<div align="center">无相关信息</div>';
            }
            $("#d_report").append(layer);
        }
    });
}

function InitTaskTimeLine(taskID, canEidtReport, pageIndex, estimatedStartTime, startIndex) {
    $("#d_more").remove();
    $("#d_timeline_more").remove();
    if (!canEidtReport) {
        //只需要取周汇报即可
        $.ajax({
            url: "/AJAX/AjaxService.aspx?method=GetTaskTimeLineList",
            cache: false,
            type: "post",
            data: {
                task_id_: taskID,
                type_: 0,
                pageIndex: pageIndex,
                pageSize: 3,
                sortField: "start_time_",
                sortOrder: "DESC"
            },
            success: function (text) {
                var obj = mini.decode(text);
                var layer = '';
                //var sTimeArr = [obj.data.length - 1];
                //var eTimeArr = [obj.data.length - 1];
                for (var i = 0; i < obj.data.length; i++) {
                    //                    if (!CheckTime(obj.data[i].start_time_, obj.data[i].end_time_, sTimeArr, eTimeArr)) continue;
                    //                    sTimeArr.push(obj.data[i].start_time_);
                    //                    eTimeArr.push(obj.data[i].end_time_);
                    layer += '<table class="tasking_report"><tr><td class="tdleft">汇报区间：</td>';
                    layer += '<td style="width: 30%;">' + new Date(obj.data[i].start_time_).Format("yyyy") + '年' + GetWeekOfYear(new Date(obj.data[i].start_time_).Format("yyyy"), new Date(obj.data[i].start_time_).Format("MM"), new Date(obj.data[i].start_time_).Format("dd")) + '周 (' + new Date(obj.data[i].start_time_).Format("MM.dd") + '~' + new Date(obj.data[i].end_time_).Format("MM.dd") + ')</td>';
                    layer += '<td class="tdleft">工作目标：</td><td><input allowinput="false" class="mini-textbox" style="width:99%" value="' + obj.data[i].target_description_ + '"/></td></tr><tr><td class="tdleft">汇报内容：</td><td colspan="3"><input class="mini-textarea" allowinput="false" style="width: 99%; height: 80px" value="' + obj.data[i].work_report_.replace(/\\r\\n/g, "\n") + '"/></td></tr></table>';
                }
                if (layer.length <= 0) {
                    layer = '<div align="center">无相关信息</div>';
                } else {
                    if (!((pageIndex + 1) * 3 >= obj.total)) {
                        layer += "<div style=\"padding-left:85px\" id=\"d_more\"><a href=\"javascript:InitTaskTimeLine(" + taskID + "," + canEidtReport + "," + (pageIndex + 1) + ",'" + new Date(estimatedStartTime).Format("yyyy-MM-dd") + "'," + startIndex + ")\">点击加载更多...</a></div>";
                    }
                }
                $("#d_timeline").append(layer);
                mini.parse(); //JS加的需要解析为miniui控件
            }
        });
    } else {
        var difWeek = GetDifWeek(estimatedStartTime, getNextDate(new Date().addDays(7), 0));
        //alert(difWeek);
        //alert(getNextDate(new Date(), 0));
        $.ajax({
            url: "/AJAX/AjaxService.aspx?method=GetTaskTimeLineHtml",
            cache: false,
            type: "post",
            data: {
                task_id_: taskID,
                diffWeek: difWeek,
                taskStartDate: new Date(estimatedStartTime).Format("yyyy-MM-dd"),
                startIndex: startIndex
            },
            success: function (text) {
                var obj = mini.decode(text);
                $("#d_timeline").append(obj.data);
                $("#time_line_count").val(obj.total);
                mini.parse();
            }
        });
    }
}
function CheckTime(startTime, endTime, sTimeArr, eTimeArr) {
    for (var i = 0; i < sTimeArr.length; i++) {
        if (sTimeArr[i] == startTime && eTimeArr[i] == endTime) {
            return false;
        }
        continue;
    }
    return true;
}

function onCloseClick(e) {
    var obj = e.sender;
    obj.setText("");
    obj.setValue("");
}

function SaveData() {
    var form = new mini.Form("#form1");
    form.validate();
    if (form.isValid() == false) return; //表单证验
    //alert(mini.get("line_id_11").getValue());
    var count = $("#time_line_count").val();
    var taskID = $("#task_id_").val();
    var lineIDArr = [];
    var typeArr = [];
    var timestringArr = [];
    var startTimeArr = [];
    var endTimeArr = [];
    var targetArr = [];
    var contentArr = [];

    var communication = '';
    // try {
    var tCommunication = document.getElementById('t_communication');
    if (tCommunication != null && tCommunication != undefined) {
        //communication = mini.get("txt_content").getValue();
        // }
        // }
        // catch (err) { }
    }
    for (var i = 0; i <= count; i++) {
        try {
            lineIDArr.push(mini.get("line_id_" + i).getValue());
            typeArr.push(mini.get("type_" + i).getValue());
            timestringArr.push(mini.get("time_string_" + i).getValue());
            startTimeArr.push(mini.get("start_time_" + i).getValue());
            endTimeArr.push(mini.get("end_time_" + i).getValue());
            targetArr.push(mini.get("target_" + i).getValue());
            contentArr.push(mini.get("content_" + i).getValue());
        } catch (err) {

        }
    }
    $.ajax({
        url: "/AJAX/AjaxService.aspx?method=EditTask",
        cache: false,
        type: "post",
        data: {
            taskID: taskID,
            lineIDs: lineIDArr,
            types: typeArr,
            time_strings: timestringArr,
            startTimes: startTimeArr,
            endTimes: endTimeArr,
            targets: targetArr,
            contents: contentArr,

            task_name_: mini.get("txt_task_name_").getValue(),
            category_id_: mini.get("txt_category_id_").getValue(),
            manage_user_id_: mini.get("txt_manage_user_name_").getValue(),
            manage_user_name_: mini.get("txt_manage_user_name_").getText(),
            estimated_start_time_: mini.get("txt_estimated_start_time_").getText(),
            estimated_end_time_: mini.get("txt_estimated_end_time_").getText(),
            task_property_: mini.get("txt_task_property_").getValue(),
            TaskStatus: mini.get("cmbTaskStatus").getValue(),
            TaskProgress: mini.get("spTaskProgress").getValue(),
            cc: mini.get("txt_cc").getValue(),
            cc_name: mini.get("txt_cc").getText(),
            description_: mini.get("txt_description_").getValue(),
            communication: communication
        },
        success: function (text) {
            if (mini.get("cmbTaskStatus").getValue() != startStatus) {
                window.top.location.reload();
            }
            alert("保存成功！");
        }
    });
}

var index = "";

function contentKeyUp(num) {
    index = num;
}

//引用下属工作汇报

function getReference() {
    if (index.length == 0) {
        alert("请先选择需要引用下属的汇报！");
    } else {
        var taskID = $("#task_id_").val();
        var time_string_ = mini.get("time_string_" + index).getValue();
        var type_ = mini.get("type_" + index).getValue();
        $.ajax({
            url: "/AJAX/AjaxService.aspx?method=GetSubContent",
            cache: false,
            type: "post",
            data: {
                taskID: taskID,
                time_string_: time_string_,
                type_: type_
            },
            success: function (text) {

                var contentAndTarget = text.split("|");
                // alert(contentAndTarget);
                mini.get("content_" + index).setValue(contentAndTarget[0]);
                mini.get("target_" + index).setValue(contentAndTarget[1]);
            }
        });
    }
}

function lookSubReport() {
    var taskID = $("#task_id_").val();
    window.open("/iTask/MyTask/SubordinatesWorkreports.aspx?task_id_=" + taskID, '_blank');
}


//任务编辑验证

function checkTarget(obj, id) {
    if (obj.value.trim().length > 0) {
        mini.get(id).setVtype("required");
        if (mini.get(id).getValue().trim().length <= 0) {
            alert("请填写工作目标！");
        }
    } else {
        //        if (mini.get(id).getValue().trim().length > 0) {
        //            alert("请填写汇报内容！");
        //            mini.get(id).setVtype("");
        //            return;
        //        }
        mini.get(id).setVtype("");
    }
}
//function checkContent(obj, id) {
//    if (obj.value.trim().length > 0) {
//        mini.get(id).setVtype("required");
//        if (mini.get(id).getValue().trim().length <= 0) {
//            alert("请填写汇报内容！");
//            return;
//        }
//    }
//    else {
//        //        if (mini.get(id).getValue().trim().length > 0) {
//        //            alert("请填写工作目标！");
//        //            mini.get(id).setVtype("");
//        //            return;
//        //        }
//        mini.get(id).setVtype("");
//    }
//}